package org.alex.samples.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.alex.samples.model.ExamScoreVO;
import org.alex.samples.model.ExamSubject;
import org.alex.samples.model.ExamSubjectVO;
import org.alex.samples.model.ExamTestVO;
import org.alex.samples.model.ExamUserVO;
import org.alex.samples.service.IExamSubjectService;
import org.alex.samples.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping(value="/student")
public class StudentController {
	
	@Autowired
	private IStudentService studentService;
	
	@Autowired
	private IExamSubjectService examSubjectService;
	
	
	//通过科目ID得到考试试题
	Map<Integer, List<ExamTestVO>> testListMap = null;
	@RequestMapping(value = "/toTest/{subId}")
	public String toTest(@PathVariable Integer subId, Model model, HttpSession session){
		ExamUserVO user = (ExamUserVO) session.getAttribute("examUser");			
		testListMap = studentService.getTestListBySubject(subId, user.getUserId());
		ExamSubject sub = examSubjectService.getExamSubjectById(subId);
		model.addAttribute("testListMap", testListMap);
		model.addAttribute("sub", sub);			
		return "student/toTest";
	}
	

	
	//计算得分
	@RequestMapping(value = "/assignment", method = RequestMethod.POST)
	public String assignment(HttpServletRequest request, String userId, Integer subId, RedirectAttributes redirectAttr){
		Map<String, String[]> parameterMap = request.getParameterMap();
		Set<Entry<String, String[]>> entrySet = parameterMap.entrySet();		
		List<Integer> answerIds = new ArrayList<Integer>();
		Map<String, String[]> testMap = new HashMap<String, String[]>();
		if(entrySet != null){
			Iterator<Entry<String, String[]>> it = entrySet.iterator();
			final String TEST_PREFIX = "test";
			while(it.hasNext()){
				Entry<String, String[]> entry = it.next();
				String key = entry.getKey();
				String[] splitArrayString = key.split("_");
				if(splitArrayString.length == 2){
					if(TEST_PREFIX.equals(splitArrayString[0])){
						answerIds.add(Integer.valueOf(splitArrayString[1]));
						testMap.put(splitArrayString[1], entry.getValue());
					}
				}
			}
			studentService.calculateScore(answerIds, testMap, userId, subId);
			return "redirect:/student/subjects";
		}
		return null;
	}
	
		
	
	//得到该学生参考的科目
	@RequestMapping(value="/subjects")
	public String toSubjects(HttpSession session,Model model){
		ExamUserVO userVO=(ExamUserVO) session.getAttribute("examUser");
		List<ExamSubjectVO> examSubjectList=studentService.getSubjectList(userVO);
		model.addAttribute("examSubjectList",examSubjectList);
		return "student/subjects";
	}
	
	//查看分数
	@ResponseBody
	@RequestMapping(value="/showScore",method=RequestMethod.POST)
	public ExamScoreVO showScore(HttpSession session,Integer scoreId){
		ExamScoreVO vo=studentService.getScoreById(scoreId);
		return vo;
	}
	
	
}
